Skip to content

Fix: crystal i REPL fails with pcre2_compile_8 on Windows MinGW#16795

Open
calospoimen wants to merge 1 commit intocrystal-lang:masterfrom
calospoimen:fix/interpreter-pcre2-windows-mingw
Open

Fix: crystal i REPL fails with pcre2_compile_8 on Windows MinGW#16795
calospoimen wants to merge 1 commit intocrystal-lang:masterfrom
calospoimen:fix/interpreter-pcre2-windows-mingw

Conversation

@calospoimen
Copy link
Copy Markdown

@calospoimen calospoimen commented Mar 27, 2026

Problem

When using crystal i (interpreter/REPL) on Windows MinGW UCRT64, any use of regular expressions raises:

undefined reference to `pcre2_compile_8' (Crystal::Loader::LoadError)

The same code works correctly with crystal run or crystal build.

Fixes #16794

Root Cause

Two bugs were found:

1. interpreter/[context.cr](http://context.cr/) — lazy loader initialization

The loader is initialized once (lazy getter) using program.lib_flags at the first c_function call. In REPL mode, pcre2 is not in lib_flags at loader init time because Regex is defined in prelude but not yet used. When the user types the first regex, pcre2 is added to lib_flags but the loader is already cached. File mode works because prelude and file are analyzed together.

2. loader/[mingw.cr](http://mingw.cr/)LoadLibraryExW handle issue

LoadLibraryExW with a bare DLL name returns a handle for already-loaded process DLLs where GetProcAddress subsequently fails. Using GetModuleHandleExW first returns the canonical handle that works correctly.

Fix

  • src/compiler/crystal/loader/[mingw.cr](http://mingw.cr/): load_dll? now uses GetModuleHandleExW first, falls back to full-path LoadLibraryExW; added resolve_dll_full_path to find the DLL in search paths and sibling bin/ directories.

  • src/compiler/crystal/interpreter/[context.cr](http://context.cr/): added dll_search_paths for Windows MinGW, tracks lib_flags at loader init time, added update_loader to dynamically load new -l libraries when lib_flags changes.

Test Case

result = "hello world" =~ /world/
puts result         # should print 6
m = /(\w+)/.match("hello")
puts m.try(&.[1])   # should print "hello"
puts "OK"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crystal i REPL raises undefined reference to pcre2_compile_8 on Windows MinGW when using regex interactively

1 participant